home *** CD-ROM | disk | FTP | other *** search
- #include "GifScan.h"
- #include "GSDrag.h"
- #include "Prefs.h"
-
- extern thePrefsHandle gPrefs;
- extern Boolean gHasDrag;
-
- // Most of this stuff from Apple's TE Sample
- pascal void PascalClikLoop(void)
- {
- WindowRef window;
- RgnHandle region;
-
- window = FrontWindow();
- region = NewRgn();
- GetClip(region);
- ClipRect(&window->portRect);
- AdjustScrollValues(window, true);
- SetClip(region);
- DisposeRgn(region);
- }
-
- pascal ProcPtr GetOldClikLoop(void)
- {
- return ((DocumentPeek)FrontWindow())->docClik;
- }
-
- asm pascal void AsmClikLoop(void)
- {
- MOVEM.L D1-D2/A1,-(SP) // D0 and A0 need not be saved
- CLR.L -(SP) // make space for procedure pointer
- JSR GetOldClikLoop // get the old clikLoop
- MOVEA.L (SP)+,A0 // into A0
- MOVEM.L (SP)+,D1-D2/A1 // restore the world as it was
-
- JSR (A0) // and execute old clikLoop
-
- MOVEM.L D1-D2/A1,-(SP) // D0 and A0 need not be saved
- JSR PascalClikLoop // do our clikLoop
- MOVEM.L (SP)+,D1-D2/A1 // restore the world as it was
- MOVEQ #1,D0 // clear the zero flag so TextEdit keeps going
- RTS
- }
-
- void GetGlobalMouse(Point *mouse)
- {
- EventRecord event;
-
- OSEventAvail(kNoEvents, &event);
- *mouse = event.where;
- }
-
- void AdjustHV(Boolean isVert, ControlHandle control,
- TEHandle docTE, Boolean canRedraw)
- {
- short value, lines, max;
- short oldValue, oldMax;
- TEPtr te;
-
- oldValue = GetCtlValue(control);
- oldMax = GetCtlMax(control);
- te = *docTE;
- if ( isVert )
- {
- lines = te->nLines;
-
- if ( *(*te->hText + te->teLength - 1) == kCrChar )
- lines += 1;
- max = lines - ((te->viewRect.bottom - te->viewRect.top) /
- te->lineHeight);
- }
- else
- max = kMaxDocWidth - (te->viewRect.right - te->viewRect.left);
-
- if ( max < 0 ) max = 0;
- SetCtlMax(control, max);
-
- te = *docTE;
- if ( isVert )
- value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
- else
- value = te->viewRect.left - te->destRect.left;
-
- if ( value < 0 ) value = 0;
- else if ( value > max ) value = max;
-
- SetCtlValue(control, value);
-
- if ( canRedraw || (max != oldMax) || (value != oldValue) )
- ShowControl(control);
- }
-
- void AdjustScrollValues(WindowRef window, Boolean canRedraw)
- {
- DocumentPeek doc;
-
- doc = (DocumentPeek)window;
- AdjustHV(true, doc->docVScroll, doc->docTE, canRedraw);
- AdjustHV(false, doc->docHScroll, doc->docTE, canRedraw);
- }
-
- Boolean IsDialogWindow(WindowRef window)
- {
- if ( window == nil )
- return(false);
- else
- return ( ((WindowPeek) window)->windowKind == dialogKind );
- }
-
- Boolean IsAppWindow(WindowRef window)
- {
- short windowKind;
-
- if ( window == nil )
- return false;
- else
- {
- windowKind = ((WindowPeek) window)->windowKind;
- return (windowKind == userKind );
- }
- }
-
- Boolean IsDAWindow(WindowRef window)
- {
- if ( window == nil )
- return false;
- else
- return ((WindowPeek) window)->windowKind < 0;
- }
-
- Boolean DoCloseWindow(WindowRef window)
- {
- TEHandle te;
-
- if ( IsDAWindow(window) )
- CloseDeskAcc(((WindowPeek) window)->windowKind);
- else if ( IsAppWindow(window) )
- {
- te = ((DocumentPeek) window)->docTE;
- if ( te != nil )
- {
- TEDispose(te);
- }
- CloseWindow(window);
- DisposePtr((Ptr) window);
- }
- return true;
- }
-
- void AdjustViewRect(TEHandle docTE)
- {
- TEPtr te;
-
- te = *docTE;
- te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
- * te->lineHeight) + te->viewRect.top;
- }
-
- void GetTERect(WindowRef window, Rect *teRect)
- {
- *teRect = window->portRect;
- InsetRect(teRect, kTextMargin, kTextMargin);
- teRect->bottom = teRect->bottom - 15;
- teRect->right = teRect->right - 15;
- }
-
- void GetLocalUpdateRgn(WindowRef window, RgnHandle localRgn)
- {
- CopyRgn(((WindowPeek) window)->updateRgn, localRgn);
- OffsetRgn(localRgn, window->portBits.bounds.left, window->portBits.bounds.top);
- }
-
- void DrawWindow(WindowRef window)
- {
- SetPort(window);
- EraseRect(&window->portRect);
- DrawControls(window);
- DrawGrowIcon(window);
- TEUpdate(&window->portRect, ((DocumentPeek) window)->docTE);
- }
-
- void AdjustScrollSizes(WindowRef window)
- {
- Rect teRect;
- DocumentPeek doc;
-
- doc = (DocumentPeek) window;
- GetTERect(window, &teRect);
- (*doc->docTE)->viewRect = teRect;
- AdjustViewRect(doc->docTE);
- if ( gHasDrag ) TEGetHiliteRgn(doc->docHiliteRgn, doc->docTE);
-
- MoveControl(doc->docVScroll, window->portRect.right - kScrollbarAdjust, -1);
- SizeControl(doc->docVScroll, kScrollbarWidth, (window->portRect.bottom -
- window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
- MoveControl(doc->docHScroll, -1, window->portRect.bottom - kScrollbarAdjust);
- SizeControl(doc->docHScroll, (window->portRect.right -
- window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
- kScrollbarWidth);
- }
-
- void AdjustScrollbars(WindowRef window, Boolean needsResize)
- {
- DocumentPeek doc;
-
- doc = (DocumentPeek) window;
-
- (*doc->docVScroll)->contrlVis = kControlInvisible;
- (*doc->docHScroll)->contrlVis = kControlInvisible;
- if ( needsResize )
- AdjustScrollSizes(window);
- AdjustScrollValues(window, needsResize);
-
- (*doc->docVScroll)->contrlVis = kControlVisible;
- (*doc->docHScroll)->contrlVis = kControlVisible;
- }
-
- void AdjustTE(WindowRef window)
- {
- TEPtr te;
-
- te = *((DocumentPeek)window)->docTE;
- TEScroll((te->viewRect.left - te->destRect.left) -
- GetCtlValue(((DocumentPeek)window)->docHScroll),
- (te->viewRect.top - te->destRect.top) -
- (GetCtlValue(((DocumentPeek)window)->docVScroll) *
- te->lineHeight),
- ((DocumentPeek)window)->docTE);
- }
-
- void ResizeWindow(WindowRef window)
- {
- AdjustScrollbars(window, true);
- AdjustTE(window);
- InvalRect(&window->portRect);
- }
-
- void DoGrowWindow(WindowRef window, EventRecord *event)
- {
- long growResult;
- Rect tempRect;
- RgnHandle tempRgn;
- DocumentPeek doc;
-
- tempRect = qd.screenBits.bounds;
- tempRect.left = kMinDocDim;
- tempRect.top = kMinDocDim;
- growResult = GrowWindow(window, event->where, &tempRect);
-
- if ( growResult != 0 )
- {
- doc = (DocumentPeek) window;
- tempRect = (*doc->docTE)->viewRect;
- tempRgn = NewRgn();
- GetLocalUpdateRgn(window, tempRgn);
- SizeWindow(window, LoWrd(growResult), HiWrd(growResult), true);
- ResizeWindow(window);
-
- SectRect(&tempRect, &(*doc->docTE)->viewRect, &tempRect);
- ValidRect(&tempRect);
- InvalRgn(tempRgn);
- DisposeRgn(tempRgn);
- }
- }
-
- void DoZoomWindow(WindowRef window, short part)
- {
- EraseRect(&window->portRect);
- ZoomWindow(window, part, window == FrontWindow());
- ResizeWindow(window);
- }
-
- static void CheckForWindowFit(void)
- {
- thePrefsHandle resHandle;
-
- // If window is (partly) outside the screen, reset to default size and position in
- // order to avoid problems when moving to smaller screens (window invisible)
- if ( !RectInRect(&(*gPrefs)->winRect, &qd.screenBits.bounds) )
- {
- resHandle = (thePrefsHandle)Get1Resource(kPrefResType, prefsResNum);
- (*gPrefs)->winRect = (*resHandle)->winRect;
- }
- }
-
- Boolean DoNew(void)
- {
- Boolean good;
- Ptr storage;
- Rect destRect, viewRect;
- DocumentPeek doc;
- Rect receiveRect;
- RgnHandle dragReceiveRgn;
- DragInfoHandle dragInfo;
- OSErr err;
- TextStyle style;
- short lineHeight, ascent;
- Boolean succes = true;
-
- storage = NewPtr(sizeof(DocumentRecord));
- if ( storage != nil )
- {
- gOutWindow = GetNewWindow(theWindResID, storage, (WindowRef)-1L);
- if ( gOutWindow != nil )
- {
- good = false;
- SetPort(gOutWindow);
- doc = (DocumentPeek) gOutWindow;
- GetTERect(gOutWindow, &viewRect);
- destRect = viewRect;
- destRect.right = destRect.left + kMaxDocWidth;
- doc->docHiliteRgn = NewRgn();
- doc->docTE = TENew(&destRect, &viewRect);
- good = doc->docTE != nil;
- if ( good )
- {
- (*(doc->docTE))->txFont = (*gPrefs)->fontNum;
- (*(doc->docTE))->txSize = (*gPrefs)->fontSize;
- TEGetStyle(0, &style, &lineHeight, &ascent, doc->docTE);
- (*(doc->docTE))->fontAscent = ascent;
- (*(doc->docTE))->lineHeight = lineHeight;
- AdjustViewRect(doc->docTE);
- TEAutoView(true, doc->docTE);
- doc->docClik = (ProcPtr) (*doc->docTE)->clickLoop;
- (*doc->docTE)->clickLoop = (TEClickLoopUPP) AsmClikLoop;
- }
- else
- return (!succes);
-
- if ( good )
- {
- doc->docVScroll = GetNewControl(rVScroll, gOutWindow);
- good = (doc->docVScroll != nil);
- }
- else
- return (!succes);
-
- if ( good)
- {
- doc->docHScroll = GetNewControl(rHScroll, gOutWindow);
- good = (doc->docHScroll != nil);
- }
- else
- return (!succes);
-
- if ( good )
- {
- CheckForWindowFit(); // Check if window will be on the screen
- SizeWindow(gOutWindow,
- (*gPrefs)->winRect.right - (*gPrefs)->winRect.left,
- (*gPrefs)->winRect.bottom - (*gPrefs)->winRect.top, true);
- MoveWindow(gOutWindow, (*gPrefs)->winRect.left, (*gPrefs)->winRect.top, true);
- AdjustScrollbars(gOutWindow, true);
- ShowWindow(gOutWindow);
- }
- else
- {
- DoCloseWindow(gOutWindow);
- return(!succes);
- }
-
- if ( gHasDrag )
- {
- dragInfo = (DragInfoHandle)NewHandle(sizeof(DragInfoRecord));
- dragReceiveRgn = NewRgn();
- receiveRect = gOutWindow->portRect;
- receiveRect.bottom -= kScrollbarAdjust;
- receiveRect.right -= kScrollbarAdjust;
- RectRgn(dragReceiveRgn, &receiveRect);
-
- (*dragInfo)->window = gOutWindow;
- (*dragInfo)->flavor = flavorTypeHFS;
- (*dragInfo)->receiveRgn = dragReceiveRgn;
- (*dragInfo)->receiveFunc = ReceiveHFSDrag;
-
- SetWRefCon(gOutWindow, (long)dragInfo);
-
- err = InitDragInfo(dragInfo);
- if ( err != noErr ) SysBeep(20);
- }
- return (succes);
- }
- else
- {
- DisposePtr(storage);
- return (!succes);
- }
- }
- else
- return(!succes);
- }
-
- void CommonAction(ControlRef control, short *amount)
- {
- short value, max;
-
- value = GetCtlValue(control);
- max = GetCtlMax(control);
- *amount = value - *amount;
- if ( *amount < 0 )
- *amount = 0;
- else if ( *amount > max )
- *amount = max;
- SetCtlValue(control, *amount);
- *amount = value - *amount;
- }
-
- pascal void VActionProc(ControlRef control, short part)
- {
- short amount;
- WindowRef window;
- TEPtr te;
-
- if ( part != 0 )
- {
- window = (*control)->contrlOwner;
- te = *((DocumentPeek) window)->docTE;
- switch ( part )
- {
- case inUpButton:
- case inDownButton:
- amount = 1;
- break;
- case inPageUp:
- case inPageDown:
- amount = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
- break;
- }
- if ( (part == inDownButton) || (part == inPageDown) )
- amount = -amount;
- CommonAction(control, &amount);
- if ( amount != 0 )
- TEScroll(0, amount * te->lineHeight, ((DocumentPeek) window)->docTE);
- }
- }
-
- pascal void HActionProc(ControlRef control, short part)
- {
- short amount;
- WindowRef window;
- TEPtr te;
-
- if ( part != 0 )
- {
- window = (*control)->contrlOwner;
- te = *((DocumentPeek) window)->docTE;
- switch ( part ) {
- case inUpButton:
- case inDownButton:
- amount = kButtonScroll;
- break;
- case inPageUp:
- case inPageDown:
- amount = te->viewRect.right - te->viewRect.left;
- break;
- }
- if ( (part == inDownButton) || (part == inPageDown) )
- amount = -amount;
- CommonAction(control, &amount);
- if ( amount != 0 )
- TEScroll(amount, 0, ((DocumentPeek) window)->docTE);
- }
- }
-
- void DoContentClick(WindowRef window, EventRecord *event)
- {
- Point mouse;
- ControlHandle control;
- short part, value;
- Boolean shiftDown;
- DocumentPeek doc;
- Rect teRect;
-
- if ( IsAppWindow(window) )
- {
- SetPort(window);
- mouse = event->where;
- GlobalToLocal(&mouse);
- doc = (DocumentPeek) window;
- GetTERect(window, &teRect);
- if ( PtInRect(mouse, &teRect) )
- {
- shiftDown = (event->modifiers & shiftKey) != 0;
- TEClick(mouse, shiftDown, doc->docTE);
- }
- else
- {
- part = FindControl(mouse, window, &control);
- switch ( part )
- {
- case 0:
- break;
-
- case inThumb:
- value = GetCtlValue(control);
- part = TrackControl(control, mouse, nil);
- if ( part != 0 )
- {
- value -= GetCtlValue(control);
- if ( value != 0 )
- if ( control == doc->docVScroll )
- TEScroll(0, value * (*doc->docTE)->lineHeight, doc->docTE);
- else
- TEScroll(value, 0, doc->docTE);
- }
- break;
-
- default:
- if ( control == doc->docVScroll )
- value = TrackControl(control, mouse, (ControlActionUPP) VActionProc);
- else
- value = TrackControl(control, mouse, (ControlActionUPP) HActionProc);
- break;
- }
- }
- }
- }
-
- void UpdateWindowFontSize(short theSize)
- {
- TextStyle style;
- short lineHeight, ascent;
- DocumentPeek doc;
- Rect aRect;
-
- if ( gOutWindow != nil )
- {
- doc = (DocumentPeek)gOutWindow;
- (*(doc->docTE))->txSize = theSize;
- TEGetStyle(0, &style, &lineHeight, &ascent, doc->docTE);
- (*(doc->docTE))->fontAscent = ascent;
- (*(doc->docTE))->lineHeight = lineHeight;
- aRect = (*(doc->docTE))->viewRect;
- EraseRect(&aRect);
- InvalRect(&aRect);
- TECalText(doc->docTE);
- AdjustViewRect(doc->docTE);
- AdjustTE(gOutWindow);
- AdjustScrollbars(gOutWindow, true);
- TESetSelect(kMaxTELength, kMaxTELength, doc->docTE); // end of window
- }
- }
-
- void UpdateWindowFont(short fontNum)
- {
- TextStyle style;
- short lineHeight, ascent;
- DocumentPeek doc;
- Rect aRect;
-
- if ( gOutWindow != nil )
- {
- doc = (DocumentPeek)gOutWindow;
- (*(doc->docTE))->txFont= fontNum;
- TEGetStyle(0, &style, &lineHeight, &ascent, doc->docTE);
- (*(doc->docTE))->fontAscent = ascent;
- (*(doc->docTE))->lineHeight = lineHeight;
- aRect = (*(doc->docTE))->viewRect;
- EraseRect(&aRect);
- InvalRect(&aRect);
- TECalText(doc->docTE);
- AdjustViewRect(doc->docTE);
- AdjustTE(gOutWindow);
- AdjustScrollbars(gOutWindow, true);
- TESetSelect(kMaxTELength, kMaxTELength, doc->docTE);
- }
- }